<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.4/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <blockquote class="layui-elem-quote layui-text">
            在此可以创建角色
        </blockquote>

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>创建角色</legend>
        </fieldset>

        <form class="layui-form">
            {{--<input type="hidden" name="_token" value="{{csrf_token()}}">--}}
            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入账号"
                           lay-reqtext="名称是必填项，岂能为空？" class="layui-input">
                </div>
            </div>
            <div id="test12" class="demo-tree-more"></div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                </div>
            </div>
        </form>

    </div>
</div>

<script src="/lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
<script src="/js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate', 'tree', 'util'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,tree = layui.tree
            , menu = []
        // //自定义验证规则
        // form.verify({
        //     name: function (value) {
        //         if (value.length < 1) {
        //             return '角色至少得1个字符啊';
        //         }
        //     }
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });

        $.get('/admin/get/all/role/menu',{},function (re) {
            menu = re;

            //基本演示
            tree.render({
                elem: '#test12'
                , data: menu
                , showCheckbox: true  //是否显示复选框
                , id: 'demoId1'
                , isJump: true //是否允许点击节点时弹出新窗口跳转
                , click: function (obj) {
                    var data = obj.data;  //获取当前点击的节点数据
                    layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                }
            });
        });
        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        var url = '/admin/role/create';
        // var data={username:$('[name="username"]').val(),password:$('[name="password"]').val(),role_id:$('[name="role_id"]').val(),warehouse_id:$('')};
        form.on('submit(demo1)', function (data) {
            var formData = data.field;
            var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
            data.field.menu_id = checkedData;

            $.ajax({
                url : url,
                data : formData,
                method: 'post',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                success: function (resp,status,xhr) {
                    if (xhr.status == 200) {
                        location.href = '/admin/role/index';
                    }
                },
                error: function (re) {
                    layer.msg(re.responseJSON.msg,{icon: 2});

                }
            })
            return false;
        });

    });

</script>

</body>
</html>